package com.github.shiqiyue.app.common.utils;

import java.util.TimeZone;

/***
 * timezone threadlocal cache
 * @author wenyao.wu
 * @since 2020/7/23
 */
public class TimeZoneThreadLocal {

    private static ThreadLocal<TimeZone> ZONE_THREAD_LOCAL = new ThreadLocal<>();

    public static void setTimeZone(TimeZone timeZone) {
        if (timeZone == null) {
            ZONE_THREAD_LOCAL.set(TimeZone.getDefault());
        } else {
            ZONE_THREAD_LOCAL.set(timeZone);
        }
    }

    public static void clear() {
        ZONE_THREAD_LOCAL.remove();
    }

    public static TimeZone getTimeZone() {
        TimeZone timeZone = ZONE_THREAD_LOCAL.get();
        if (timeZone == null) {
            return TimeZone.getDefault();
        }
        return timeZone;
    }
}
